package _binary_search;

/**
 * 153. 寻找旋转排序数组中的最小值
 */
public class No153 {
    /*
      一、固定右值
     */

    /**
     * 1. 闭区间
     */
    public int findMin11(int[] nums) {
        int n = nums.length;
        int left = 0, right = n - 2;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[n - 1]) left = mid + 1;
            else right = mid - 1;
        }
        return nums[left];
    }

    /**
     * 2. 左闭右开
     */
    public int findMin12(int[] nums) {
        int n = nums.length;
        int left = 0, right = n - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[n - 1]) left = mid + 1;
            else right = mid;
        }
        return nums[left];
    }

    /**
     * 3. 开区间
     */
    public int findMin13(int[] nums) {
        int n = nums.length;
        int left = -1, right = n - 1;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[n - 1]) left = mid;
            else right = mid;
        }
        return nums[right];
    }

    /*
      二、动态右值
     */

    /**
     * 2. 左闭右开（右值为右开区间值）
     */
    public int findMin22(int[] nums) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[right]) left = mid + 1;
            else right = mid;
        }
        return nums[left];
    }

    /**
     * 3. 开区间（右值为右开区间值）
     */
    public int findMin23(int[] nums) {
        int left = -1, right = nums.length - 1;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums[right]) left = mid;
            else right = mid;
        }
        return nums[right];
    }
}
